// testCpuUsage.cpp : Defines the entry point for the console application.
//

#include "stdafx.h"

#include <Windows.h>
#include <string>
#include <iostream>

unsigned int GetCPUTickCount(){ 
	_asm 
	{ 
		rdtsc; 
	} 
}

int _tmain(int argc, _TCHAR* argv[])
{
	// Method one
	/*
	for (;;)
	{
		for (int i = 0; i < 9600000; i ++)
		{

		}
		::Sleep(10);
	}
	*/

	// Method two
	/*
	int busyTime = 10;	// ms
	int idleTime = busyTime;
	INT64 startTime = 0;
	while (true)
	{
		startTime = GetTickCount();

		// Busy loop
		while ((GetTickCount() - startTime) <= busyTime);

		// Idle loop
		::Sleep(idleTime);
	}
	*/

	// This method is available for use only in Windows Server 2003.
	// Retrieves the processor information for the node.
	// IClusCfgNodeInfo::GetProcessorInfo();

	
	// Use LPSYSTEM_INFO to get system info.
	/*
	LPSYSTEM_INFO lpSysInfo = NULL;
	lpSysInfo = (LPSYSTEM_INFO)malloc(sizeof(SYSTEM_INFO));
	ZeroMemory(lpSysInfo, sizeof(SYSTEM_INFO));
	GetSystemInfo(lpSysInfo);
	free(lpSysInfo);
	*/

	std::string processorType;

	// Use reference of SYSTEM_INFO to get system info.
	/*
	SYSTEM_INFO sysInfo;
	GetSystemInfo(&sysInfo);

	// Processor type.
	if (sysInfo.dwProcessorType == PROCESSOR_INTEL_386)
	{
		processorType = "Intel 386";
	}
	else if (sysInfo.dwProcessorType == PROCESSOR_INTEL_486)
	{
		processorType = "Intel 486";
	}
	else if (sysInfo.dwProcessorType == PROCESSOR_INTEL_PENTIUM)
	{
		processorType = "Intel Pentium (586)";
	}

	// Number of processors.
	DWORD dwNubmerOfProcessors = sysInfo.dwNumberOfProcessors;

	// Active processor mask
	DWORD_PTR pActiveProcessorMask = NULL;
	pActiveProcessorMask = sysInfo.dwActiveProcessorMask;

	std::cout << processorType.c_str() << std::endl;
	std::cout << dwNubmerOfProcessors << std::endl;
	*/

	HANDLE currentThread = GetCurrentThread();
	HANDLE currentProcess = GetCurrentProcess();
	DWORD dwCurTID = GetCurrentThreadId();
	DWORD dwCurPID = GetCurrentProcessId();

	SetThreadAffinityMask(currentThread, 0x00000001);
	SetProcessAffinityMask(currentProcess, 0x00000001);
	//SetThreadAffinityMask((HANDLE)sysInfo.dwActiveProcessorMask, 0x00000001);
	//SetProcessAffinityMask((HANDLE)sysInfo.dwActiveProcessorMask, 0x00000001);

	UINT cpuTick = GetCPUTickCount();

	int busyTime = 10;	// ms
	int idleTime = 10;
	INT64 startTime = 0;
	while (true)
	{
		startTime = GetTickCount();

		// Busy loop
		while ((GetTickCount() - startTime) <= busyTime);

		// Idle loop
		::Sleep(idleTime);
	}

	return 0;
}